package org.jeecg.modules.lab.items.collet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.collet.entity.LabItemsCollet;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.collet.service.ILabItemsColletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 模板收藏表
 * @Author: jeecg-boot
 * @Date:   2024-08-05
 * @Version: V1.0
 */
@Api(tags="模板收藏表")
@RestController
@RequestMapping("/items/collet")
@Slf4j
public class LabItemsColletController extends JeecgController<LabItemsCollet, ILabItemsColletService> {
	@Autowired
	private ILabItemsColletService labItemsColletService;
	@Autowired
	private ILabCronJobService labCronJobService;
	/**
	 * 分页列表查询
	 *
	 * @param labItemsCollet
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "模板收藏表-分页列表查询")
	@ApiOperation(value="模板收藏表-分页列表查询", notes="模板收藏表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabItemsCollet>> queryPageList(LabItemsCollet labItemsCollet,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabItemsCollet> queryWrapper = QueryGenerator.initQueryWrapper(labItemsCollet, req.getParameterMap());
		Page<LabItemsCollet> page = new Page<LabItemsCollet>(pageNo, pageSize);
		IPage<LabItemsCollet> pageList = labItemsColletService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param labItemsCollet
	 * @return
	 */
	@AutoLog(value = "模板收藏表-添加")
	@ApiOperation(value="模板收藏表-添加", notes="模板收藏表-添加")
//	@RequiresPermissions("collet:lab_items_collet:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabItemsCollet labItemsCollet) {
		labItemsColletService.save(labItemsCollet);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param labItemsCollet
	 * @return
	 */
	@AutoLog(value = "模板收藏表-编辑")
	@ApiOperation(value="模板收藏表-编辑", notes="模板收藏表-编辑")
//	@RequiresPermissions("collet:lab_items_collet:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabItemsCollet labItemsCollet) {
		labItemsColletService.updateById(labItemsCollet);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "模板收藏表-通过id删除")
	@ApiOperation(value="模板收藏表-通过id删除", notes="模板收藏表-通过id删除")
//	@RequiresPermissions("collet:lab_items_collet:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labItemsColletService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "模板收藏表-批量删除")
	@ApiOperation(value="模板收藏表-批量删除", notes="模板收藏表-批量删除")
//	@RequiresPermissions("collet:lab_items_collet:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labItemsColletService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 收藏
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实验项目信息表-收藏")
	@ApiOperation(value = "实验项目信息表-收藏", notes = "实验项目信息表-收藏")
	@RequestMapping(value = "/addCollect", method = {RequestMethod.GET, RequestMethod.POST})
	public Result<String> addCollect(@RequestParam(name = "id", required = true) String id,
									 @RequestParam(name = "userId", required = true) String userId,
								     @RequestParam(name = "collect", required = true) String collect) {
		QueryWrapper<LabItemsCollet> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("item_id",id);
		queryWrapper.eq("user_id",userId);
		List<LabItemsCollet> itemsCollets = labItemsColletService.list(queryWrapper);
		LabItemsCollet labItemsCollet = new LabItemsCollet();
		if(itemsCollets.isEmpty()){
			labItemsCollet.setItemId(id);
			labItemsCollet.setUserId(userId);
			labItemsCollet.setCollect(collect);
			labItemsColletService.save(labItemsCollet);
			if (MyEnum.sync) {
				long time = new Date().getTime();
				LabCronJob job = new LabCronJob("lab_items_collet", "0", time, 1);
				labCronJobService.save(job);
			}
		}else {
			labItemsCollet = itemsCollets.get(0);
			labItemsCollet.setCollect(collect);
			labItemsColletService.updateById(labItemsCollet);
			if (MyEnum.sync) {
				long time = new Date().getTime();
				LabCronJob job = new LabCronJob("lab_items_collet", "1", time, 1);
				labCronJobService.save(job);
			}
		}
		return Result.OK("收藏成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "模板收藏表-通过id查询")
	@ApiOperation(value="模板收藏表-通过id查询", notes="模板收藏表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabItemsCollet> queryById(@RequestParam(name="id",required=true) String id) {
		LabItemsCollet labItemsCollet = labItemsColletService.getById(id);
		if(labItemsCollet==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labItemsCollet);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param labItemsCollet
    */
//    @RequiresPermissions("collet:lab_items_collet:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsCollet labItemsCollet) {
        return super.exportXls(request, labItemsCollet, LabItemsCollet.class, "模板收藏表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("collet:lab_items_collet:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsCollet.class);
    }

}
